package com.example.touch.mediator;

import java.util.Collection;
import java.util.HashMap;

import com.example.touch.communication.ILocationPersistentAdapter;
import com.example.touch.location.IEvaluableObjectLocation;

public class LocationsRepository
{
	private ILocationPersistentAdapter locationPersistentAdapter;
	private HashMap<String, IEvaluableObjectLocation> locationsMap;
	
	public LocationsRepository(ILocationPersistentAdapter adapter)
	{
		this.locationPersistentAdapter = adapter;
		initializeLocationsMap(this.locationPersistentAdapter);
	}
	
	public IEvaluableObjectLocation getLocation(String locationObjectID)
	{
		if(locationsMap.containsKey(locationObjectID))
			return locationsMap.get(locationObjectID);
		return null;
	}
	
	public void putLocation(IEvaluableObjectLocation locationObj)
	{
		locationsMap.put(locationObj.getObjectLocationID(), locationObj);
	}
	
	private void initializeLocationsMap(ILocationPersistentAdapter adapter)
	{
		locationsMap = new HashMap<String, IEvaluableObjectLocation>();
		Collection<IEvaluableObjectLocation> locationObjects = locationPersistentAdapter.deserializeObjectLocations();
		
		for(IEvaluableObjectLocation locationObj : locationObjects)
		{
			locationsMap.put(locationObj.getObjectLocationID(), locationObj);
		}
	}
}
